

%call testIaaS(3)
function testIaaS(num_of_hypers)
addpath('opera'); 
addpath('xml_io_tools')

% setdynpath
% javaclasspath
% javaaddpath({
%    '/home/zigorat/workspace2/model/dist/model.jar', ...
%    '/home/zigorat/workspace2/Opera/dist/opera.jar'});
% 
% % import aperaTests.common.Model;
% import model.Model;
% import aperaTests.testIaaSRequestHandling.*


% '/home/zigorat/project/opera_confings/Simple_DB_Operations.model.pxl'); 
% '/home/zigorat/project/aperaTests/src/aperaTests/testIaaSRequestHandling/ ThreeHypers.xml');
opM=OpModel();
opM.buildtest();
opM.solve(); 


% samples=100;
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%
% hyper_capacity=[900,1800,3600];%ones(num_of_hypers,1).*500;
% %%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% instance_per_hyper=zeros(samples,num_of_hypers);
% hyper_left_capacity=zeros(samples,num_of_hypers);
% hyper_left_capacity(1,:)=hyper_capacity;
% 
% tmp=[]
% 
% for i=1:samples
%     % set request inter-arrival time as service time of the center
%     rate=5+3.9*sin(i*10*pi/180); %15+14*sin((i*4*pi/180)+rand)+rand*6
%     request_inter_arrival=get_interarrival_time_by_arrival_rate(rate);
%     tmp=[tmp,rate];
%     
%     % set the proposal rate for each hyper (open model) or the number of proposals
%     %hyper_proposal_num=[3,2.7,2];
%     hyper_proposal_num=[];
%     left_capacity=[];
%     for j=1:num_of_hypers
%         left_capacity(j)=hyper_capacity(j)-instance_per_hyper(i,j);
%     end
% 
%     for j=1:num_of_hypers
%         hyper_proposal_num(j)=left_capacity_based_proposal_rate(...
%             left_capacity(j),      ...%hyper left capacity
%             sum(left_capacity)     ...%rack_capacity)
%         );
%     end
%     hyper_proposal_num
%     
%     hyper_think_time=[0,0,0];
% 
%     % set the amount of time between each two samples
%     sampling_gap=3;
% 
%     % solve the model, compute the throughput for that period, and see how many requests we handled (x*t)
%     % add that to hypers and generate a vector entry for corent caoacities
%     % returns float[]
%     for j=1:num_of_hypers
%         m.setDemand('InstanceRequest',['propose',num2str(j)],'CPUDemand',num2str(request_inter_arrival));
%         m.setPerClassPopulation(0, ['propose',num2str(j)], num2str(hyper_proposal_num(j)));
%         m.setPerClassThinkTime(['propose',num2str(j)], num2str(hyper_think_time(j)));
%     end
% 

%     m.solve();
%     result_file = strcat(pwd,'/opera/output/output.xml');  
%     m.SaveResultsToXmlFile(result_file); 
%     [tree treeName] = xml_read (result_file);
%     disp([treeName{1} ' ='])
%     gen_object_display(tree); 

%     for j=1:num_of_hypers
%         instance_per_hyper(i+1,j)=instance_per_hyper(i,j)+m.getThroughput(0, ['propose',num2str(j)])*sampling_gap; 
%         hyper_left_capacity(i+1,j)=hyper_capacity(j)-instance_per_hyper(i+1,j);
%     end
% end
% figure;
% plot(hyper_left_capacity)
% hold on
% figure
% plot(tmp)
end

function setdynpath
javaclasspath({
    'C:\my\project\eclipse\aperaTests\bin',...
    });
end

function [interarrival]=get_interarrival_time_by_arrival_rate(lambda)
    interarrival=-log(rand)/lambda;
end

function [proposal_num]=left_capacity_based_proposal_rate(hyper_capacity,rack_capacity)
    proposal_num=(hyper_capacity/rack_capacity)*4;
    %proposal_num=round(rand*2)+1;
end

%regression measure

% value(2)=javaMethod('getPerClassPopulation',lQMModel,0,browse);
function [throughputs]=invoke_model(...
    m...
    ,request_inter_arrival  ...%int
    ,hyper_proposal_num     ...%int[]
    ,hyper_think_time       ...%int[]
    ,sampling_gap           ...%int
    )
end

